package com.parking.common.web.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
//开启swagger
@EnableSwagger2
@Configuration
@ConfigurationProperties(prefix = "swagger")
@Data
public class SwaggerConfig {
    //配置主要就是设置Docket对象的一些参数
    private String name;
    private String url;
    private String email;
    private String desc;
    private String title;
    private String basePackage;

    @Bean
    public Docket createDocket() {
        //常见文档对象，选择文档类型
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        //创建文档对象里面的文档基本信息
        ApiInfo apiInfo = new ApiInfoBuilder()
                .contact(new Contact(name, url, email))
                .description(desc)
                .title(title)
                .build();
        //把上面的文档对象设置进去以及设置对生成文档的接口路径扫描
        docket.apiInfo(apiInfo)
                .select().apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(getParameterList());
        return docket;
    }

    @Bean
    public List<Parameter> getParameterList(){
        ParameterBuilder token = new ParameterBuilder();
        List<Parameter> params = new ArrayList<>();
        token.name("authorization")
             .description("令牌")
             .modelRef(new ModelRef("string"))
             .parameterType("header")
             .required(false)
             .build();
        params.add(token.build());
        return params;
    }
}
